package com.sjy.project.ent.data.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.validation.Validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sjy.project.ent.data.mapper.EntDataMapper;
import com.sjy.project.ent.data.domain.EntData;
import com.sjy.project.ent.data.service.IEntDataService;
import com.sjy.project.system.user.service.UserServiceImpl;
import com.sjy.common.constant.UserContext;
import com.sjy.common.entity.SecuryUserDetail;
import com.sjy.common.exception.BusinessException;
import com.sjy.common.exception.ServiceException;
import com.sjy.common.utils.GPSUtil;
import com.sjy.common.utils.StringUtils;
import com.sjy.common.utils.bean.BeanValidators;
import com.sjy.common.utils.security.ShiroUtils;
import com.sjy.common.utils.text.Convert;

/**
 * 企业档案Service业务层处理
 * 
 * @author ljb
 * @date 2022-06-04
 */
@Service
public class EntDataServiceImpl implements IEntDataService 
{
	private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private EntDataMapper entDataMapper;
    @Autowired
    protected Validator validator;

    /**
     * 查询企业档案
     * 
     * @param id 企业档案主键
     * @return 企业档案
     */
    @Override
    public EntData selectEntDataById(Long id)
    {
        return entDataMapper.selectEntDataById(id);
    }

    /**
     * 通过社会统一代码查询企业档案信息
     * @param creditCode
     * @return
     */
    public EntData selectEntDataByCreditCode(String creditCode,Integer dataYear) {
    	return entDataMapper.selectEntDataByCreditCode(creditCode,dataYear);
    }
    /**
     * 查询企业档案列表
     * 
     * @param entData 企业档案
     * @return 企业档案
     */
    @Override
    public List<EntData> selectEntDataList(EntData entData)
    {
        return entDataMapper.selectEntDataList(entData);
    }

    /**
     * 新增企业档案
     * 
     * @param entData 企业档案
     * @return 结果
     */
    @Override
    public int insertEntData(EntData entData)
    {
        return entDataMapper.insertEntData(entData);
    }

    /**
     * 修改企业档案
     * 
     * @param entData 企业档案
     * @return 结果
     */
    @Override
    public int updateEntData(EntData entData)
    {
        return entDataMapper.updateEntData(entData);
    }

    /**
     * 批量删除企业档案
     * 
     * @param ids 需要删除的企业档案主键
     * @return 结果
     */
    @Override
    public int deleteEntDataByIds(String ids)
    {
        return entDataMapper.deleteEntDataByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除企业档案信息
     * 
     * @param id 企业档案主键
     * @return 结果
     */
    @Override
    public int deleteEntDataById(Long id)
    {
        return entDataMapper.deleteEntDataById(id);
    }
    
    /**
     * 导入企业数据
     * 
     * @param entList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public String importEnt(List<EntData> entList, Boolean isUpdateSupport)
    {
        if (StringUtils.isNull(entList) || entList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        SecuryUserDetail user = UserContext.getUserSession();
        String operName = user.getLoginName();
        for (EntData ent : entList)
        {
            try
            {
                // 验证是否存在这个防疫点
                EntData u = entDataMapper.selectEntDataByCreditCode(ent.getCreditCode(),ent.getDataYear());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, ent);
                    
                    ent.setCreatedBy(operName);
                    this.insertEntData(ent);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、企业 " + ent.getEntName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, ent);
                  
                    ent.setId(u.getId());
                    ent.setUpdatedBy(operName);
                    this.updateEntData(ent);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、企业 " + ent.getEntName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、企业 " + ent.getEntName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、企业 " + ent.getEntName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
    
}
